<?php
namespace app\index\controller;

use app\common\model\Software;
use think\facade\Route;

/**
 * Class Index
 * @package app\index\controller
 * @author jwj <jwjbjg@gmail.com>
 * @copyright jwj
 */
class Index
{
    /**
     * 首页
     * @return \think\response\View
     */
    public function index()
    {
        $query = Software::order('type', 'asc')
            ->order('version', 'desc')
            ->where('latest', 1);

        $result = $query->cache(3600)->select();

        return view('', [
            'result' => $result,
        ]);
    }

    /**
     * 更新日志
     * @return \think\response\View
     */
    public function log()
    {
        return view();
    }

    /**
     * 帮助页
     * @return \think\response\View
     */
    public function help()
    {
        return view();
    }

    /**
     * 最近更新
     * @return \think\response\View
     */
    public function news()
    {
        $result = Software::order('create_time', 'desc')->paginate(20);

        return view('', ['result' => $result]);
    }

    /**
     * 关于
     * @return \think\response\View
     */
    public function about()
    {
        return view();
    }

    /**
     * 生命周期
     * @return \think\response\View
     */
    public function endoflife()
    {
        $client = new \GuzzleHttp\Client(['base_uri' => 'https://endoflife.date/api/', 'timeout'  => 15]);

        $cacheKey = 'endoflife-apache';
        if (\think\facade\Cache::has($cacheKey)) {
            $apache = \think\facade\Cache::get($cacheKey);
        } else {
            $response = $client->get('apache.json');
            $apache = json_decode($response->getBody(), true);
            if (JSON_ERROR_NONE !== json_last_error()) {
                $apache = [];
            } else {
                \think\facade\Cache::set($cacheKey, $apache, 86400);
            }
        }

        $cacheKey = 'endoflife-mysql';
        if (\think\facade\Cache::has($cacheKey)) {
            $mysql = \think\facade\Cache::get($cacheKey);
        } else {
            $response = $client->get('mysql.json');
            $mysql = json_decode($response->getBody(), true);
            if (JSON_ERROR_NONE !== json_last_error()) {
                $mysql = [];
            } else {
                \think\facade\Cache::set($cacheKey, $mysql, 86400);
            }
        }

        $cacheKey = 'endoflife-mariadb';
        if (\think\facade\Cache::has($cacheKey)) {
            $mariadb = \think\facade\Cache::get($cacheKey);
        } else {
            $response = $client->get('mariadb.json');
            $mariadb = json_decode($response->getBody(), true);
            if (JSON_ERROR_NONE !== json_last_error()) {
                $mariadb = [];
            } else {
                \think\facade\Cache::set($cacheKey, $mariadb, 86400);
            }
        }

        $cacheKey = 'endoflife-php';
        if (\think\facade\Cache::has($cacheKey)) {
            $php = \think\facade\Cache::get($cacheKey);
        } else {
            $response = $client->get('php.json');
            $php = json_decode($response->getBody(), true);
            if (JSON_ERROR_NONE !== json_last_error()) {
                $php = [];
            } else {
                \think\facade\Cache::set($cacheKey, $php, 86400);
            }
        }

        return view('', ['apache' => $apache, 'mysql' => $mysql, 'mariadb' => $mariadb, 'php' => $php]);
    }

    /**
     * Sitemap地图生成
     * @return string
     */
    public function sitemap()
    {
        $xml = '';

        $result = Software::field(['id', 'update_time'])->cursor();
        foreach ($result as $res) {
            $xml .= sprintf(
                '<url><loc>%s</loc><lastmod>%s</lastmod></url>' . PHP_EOL,
                Route::buildUrl('Software/info', ['id' => $res->id])->suffix(true)->domain(true),
                date('c', strtotime($res->update_time))
            );
        }

        $result = \app\common\model\Article::where('status', 1)->field(['id', 'update_time'])->cursor();
        foreach ($result as $res) {
            $xml .= sprintf(
                '<url><loc>%s</loc><lastmod>%s</lastmod></url>' . PHP_EOL,
                Route::buildUrl('Article/info', ['id' => $res->id])->suffix(true)->domain(true),
                date('c', strtotime($res->update_time))
            );
        }

        return '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL .
            '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . PHP_EOL . $xml . PHP_EOL . '</urlset>';
    }
}
